﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SwAddinHelpers.Contracts;
using SwAddinHelpers.Attributes;

using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Design;
using System.ComponentModel.Composition.Hosting;

namespace SwAddinHelpers.AddinManager
{
    public partial class AddinManager : Form
    {
        private CompositionContainer _container = null;

        public AddinManager(CompositionContainer container)
        {
            InitializeComponent();

            _container = container;
        }

        private void AddinManager_Load(object sender, EventArgs e)
        {
            listView1.Items.Clear();

            if (_container != null)
            {
                 IEnumerable<IExternalUserControl> externalControls = _container.GetExportedValues<IExternalUserControl>();

                 foreach (IExternalUserControl ctl in externalControls)
                 {
                      HostedAsAttribute[] attribs = null;
                      string hosts = "";
                      try
                      {
                          attribs = ctl.ExternalUserControl.GetType().GetCustomAttributes(typeof(HostedAsAttribute), false);
                          foreach (HostedAsAttribute a in attribs)
                          {
                              hosts += a.GetType().Name + " ";
                          }
                      }
                      catch { }

                     ListViewItem lvitem = new ListViewItem("");
                     lvitem.Checked = ctl.ExternalUserControl.IsCreated;
                     lvitem.SubItems.Add(ctl.ExternalUserControl.GetType().ToString());
                     lvitem.SubItems.Add(hosts);
                     listView1.Items.Add(lvitem);
                 }
            }
        }
    }
}
